<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/2 0002
 * Time: 11:35
 */

namespace app\api\controller;


use app\admin\model\Articledetail;

class Article extends BaseController
{
    /**
     * @return \think\response\Json
     * 文章列表
     */
    public function getArticle()
    {
        $param = self::getHttpParam();
        $start = 0;
        $length = 20;
        if (empty($param->tenantId)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'tenantId不能为空');
        } else {
            if (!empty($param->start)) {
                $start = $param->start;
            }
            if (!empty($param->length)) {
                $length = $param->length;
            }

            $where = ['tenantId' => $param->tenantId];
            if(!empty($param->search)) {  //根据关键字搜索
                $where['title'] = ['like','%'.$param->search.'%'];
            }

            $article = \app\admin\model\Article::where($where)->limit($start, $length)->select();
            if (empty($article)) {
                return show(config('status.EMPTY_STATUS'), self::NOT_DATA, '未查到文章信息');
            } else {
                return show(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, $article);
            }
        }
    }

    /**
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 文章详情
     */
    public function getArticleDetail()
    {
        $param = self::getHttpParam();
        if (empty($param->articleId)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'articleId不能为空');
        } else {
            $article = Articledetail::alias('a')->join('article b','a.articleId=b.id')->where('articleId', $param->articleId)->find();
            if (empty($article)) {
                return show(config('status.EMPTY_STATUS'), self::NOT_DATA, '未查到文章信息');
            } else {  //每查看一次，阅读数量加1
                \app\admin\model\Article::where('id', $param->articleId)->setInc('readCount');
                $article['readCount'] += 1;
                return show(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, $article);
            }
        }
    }

}